﻿using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PizzaSystem
{
    public partial class BuyInventory : Form
    {
        public BuyInventory(Inventory inventory)
        {
            InitializeComponent();
            this.inventory = inventory;
            load();
        }

        InventoryBill inventoryBill = new InventoryBill();

        Inventory inventory;
        
        private void load()
        {
            i_id.Text = inventory.I_id.ToString();
            i_name.Text = inventory.I_name.ToString();
            i_price.Text = inventory.I_price.ToString();
        }

        private void i_num_TextChanged(object sender, EventArgs e)
        {
            if (i_num.Text == "")
            {
                t_price.Text = "0.00";
                return;
            }
            if (long.Parse(i_num.Text) > 1000000)
            {
                MessageBox.Show("一次最多只能订购1000000份");
                return;
            }
            t_price.Text = (decimal.Parse(i_price.Text) * int.Parse(i_num.Text)).ToString("0.00");
        }

        private void submit_Click(object sender, EventArgs e)
        {
            if (i_num.Text == "0" || i_num.Text == "")
            {
                MessageBox.Show("请先选择订购数量...");
                return;
            }
            SqlDao sqlDao = new SqlDao();
            inventoryBill.Ib_id = Utils.getTimeTicks();
            inventoryBill.Ib_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            inventoryBill.Ib_money = decimal.Parse(t_price.Text);
            inventoryBill.Ib_num = int.Parse(i_num.Text);
            inventoryBill.V_id = inventory.V_id;
            inventoryBill.I_id = inventory.I_id;
            if (sqlDao.insertInventoryBill(inventoryBill))
            {
                MessageBox.Show("订单已生成...");
            }
            else
            {
                MessageBox.Show("订单提交失败...");
            }
        }
    }
}
